/**
 * @fileOverview
 * WashizuMahjong.
 * http://code.google.com/p/washizu-mahjong/
 */
/**
 * @constructor
 * @this {MahjongTile}
 * @param {String} t_name	the Mahjong tile's name, e.g. 南, 三餅
 * @param {String} type		the tile type, e.g. 字牌, 萬
 * @param {String} picText	the XML unicode for the tile, e.g. <span class="unicode">&#126976;</span>(&amp;#126976;)
 * @param {Boolean} [hidden=false]	 Whether this tile is a hidden tile
 * @param {String} [pic]		 picture the url of the tile picture.
 * @property {Number} id	Tile ID, it will equal the tile index in {@link DEFAULT_TILES}.
 * @property {String} tileName	the Mahjong tile's name.
 * @property {String} tilePicText	The tile's Unicode Characters. It will be set as XML Characters, such as &amp;#126976;.
 * @property {String} mahjongPic	picture url
 * @property {Boolean} [hiddenTile=false]	Whether this tile is a hidden tile. It will be set as the param hidden
 * @property {String} tileType	The type of the tile, such as "餅","萬","索","字牌".
 * @property {Number} typeCode
 * @property {Number} simpleNum
 * @property {Number} wordNum
 * @property {String} tilePicText_UTF_16	The tile's Unicode Characters. This string is the same as {@link #tilePicText} but it is a UTF-16 string, such as 0xD83C0xDC00.
 * @property {Boolean} [isRedDora=false]
 * @property {Boolean} [isRinshanTile=false]	Whether the tile is the extra tile from the end of the wall.
 * @property {Number} [pickingTileNum=0]		The number of the tiles picked up.
 * @property {Number} [jun=-1]			The 巡（jun） when the tile discard.
 * @property {Object} owner An object save the detail of the tile owner
 */
function MahjongTile(t_name, type,picText, hidden, pic, isRedDora){
	if(!t_name || !type || !picText)return;
	var chinesetext = ["一","二","三","四","五","六","七","八","九"];
	this.tileName = t_name;
	this.tilePicText = picText;
	this.hiddenTile = !!hidden;
	this.mahjongPic = pic;
	this.tileType = type;

	var tl = ["餅","萬","索","字牌"];
	var word = ["東","南","西","北","白","發","中"];
	this.typeCode = tl.indexOf(type);
	this.id = parseInt(Math.random() * 100 + 134);

	if(t_name.length == 2){
		var chNum = t_name[0];
		this.simpleNum = chinesetext.indexOf(chNum) + 1;
		this.wordNum = -1;
	}else{
		this.simpleNum = -1;
		this.wordNum = word.indexOf(t_name);
	}
	this.isRedDora = !!isRedDora;
	this.isRinshanTile = false;


	var utf16Num = (parseInt(picText.replace("&#","").replace(";","").replace(/ /g, "")) - 70656).toString(16); //&#126976; => 0xD83C 0xDC00 //50752
	var utf16Text = "";
	eval("utf16Text = \"\\uD83C\\u" + utf16Num + "\"");
	this.tilePicText_UTF_16 = utf16Text;
	var str = "[Mahjong Tile";
	str += ": \t" + t_name + "（"+ utf16Text + "）";
	if(isRedDora) str += "（Red Dora）";
	if(hidden) str+= "（hidden）";
	str+= "]";
	this.toString = function(){return str};
}
MahjongTile.prototype.pickingTileNum = 0;
MahjongTile.prototype.jun = -1;
/**
 * @function
 * @param {MahjongTile} m
 * @return  {Boolean}
 */
MahjongTile.prototype.isSameTile= function(m){
	if(!m || !(m.constructor == MahjongTile))return false;
	return (m.simpleNum == this.simpleNum) && (m.wordNum == this.wordNum) && (m.typeCode == this.typeCode);
}
/**
 * @function
 * @param {MahjongTile} m
 * @return  {Boolean}
 */
MahjongTile.prototype.isSameTileType= function(m){
	if(!m || !(m instanceof MahjongTile))return false;
	return (m.typeCode == this.typeCode);
}
MahjongTile.prototype.__defineGetter__("isGreenTile", function(){
	switch(this.typeCode){
		case MahjongType.Sou:
			switch(this.simpleNum){
				case 2:
				case 3:
				case 4:
				case 6:
				case 8:
					return true;
					break;
				default :
					return false;
			}
			break;
		case MahjongType.Word:
			if(this.wordNum == MahjongWordNumCode.Hatsu)
				return true;
			return false;
			break;
		default:
			return false
			break;
	}
});
MahjongTile.prototype.__defineGetter__("isYaoChuuTile", function(){
	if(this.typeCode == MahjongType.Word || this.simpleNum == 1 || this.simpleNum == 9){
		return true
	}
	return false;
});
